/*
 * ShowRequests.java
 * 
 * Version information
 * 
 * Mar 29, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.icontact.webapp.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import net.amsoft.iservice.icontact.hibernate.dataObject.LoginData;
import net.amsoft.iservice.icontact.hibernate.dataObject.MessageData;
import net.amsoft.iservice.icontact.service.IContact;
import static net.amsoft.iservice.icontact.util.IContactConstants.*;

/**
 * The ShowRequests action class shows all the request messages received, the
 * corresponding replies are also shown. This class fetches only a limited no.
 * of messages at a time, and further fetching is done when the request comes
 * in.
 * 
 * @author rahul
 * 
 */
public class ShowRequests extends BaseAction {

	public static final Logger oLogger = Logger.getLogger(ShowRequests.class);

	public ActionForward process(ActionMapping oMapping, ActionForm oForm,
			HttpServletRequest oRequest, HttpServletResponse oResponse)
			throws Exception {
		oLogger.info("process() : entry");
		LoginData oLoginData = (LoginData) oRequest.getSession(false)
				.getAttribute(SESSION_LOGIN_OBJ);
		if (oLoginData.isLoginStatus() == false) {
			return oMapping.findForward(FWD_CHKLOGIN);
		}
		// set the request page index
		int iNextIndex = 0;
		try {
			iNextIndex = Integer.parseInt(oRequest
					.getParameter(REQ_CUR_MSG_PAGE)) - 1;
			iNextIndex = (iNextIndex < 0) ? 0 : iNextIndex;
			oLoginData.setCurReqIndex(iNextIndex);
		} catch (Exception e) {
			oLogger.warn("process() : Exception: " + e.getMessage());
			oLoginData.setCurReqIndex(0);
		}

		// set the reply page index
		String sReplyCount = oRequest.getParameter(REQ_CUR_REPLY_PAGE);
		try {
			if (sReplyCount == null || Integer.parseInt(sReplyCount) <= 0) {
				sReplyCount = "1";
			} else {
				sReplyCount = String.valueOf(Integer.parseInt(sReplyCount
						.trim()));
			}
		} catch (Exception e) {
			oLogger.warn("process() : Exception: " + e.getMessage()
					+ ", Setting ReplyCount=1");
			sReplyCount = "1";
		}
		oRequest.setAttribute(REQ_CUR_REPLY_PAGE, sReplyCount);

		// set the expanded reply page index
		String sExpRow = oRequest.getParameter(REQ_EXP_ROW_ID);
		if (sExpRow != null && sExpRow.trim().length() > 0) {
			try {
				sExpRow = String.valueOf(Integer.parseInt(sExpRow.trim()));
			} catch (Exception e) {
				oLogger.warn("process() : Exception : " + e.getMessage());
			}
		} else {
			sExpRow = "";
		}
		oRequest.setAttribute(REQ_EXP_ROW_ID, sExpRow);

		String sSynonym = oRequest.getParameter(SESSION_SYN);
		try {
			if (sSynonym == null
					|| !oLoginData.getAllSynonyms().contains(sSynonym)) {
				Object sSessionSyn = oRequest.getSession(false).getAttribute(
						SESSION_SYN);
				if (sSessionSyn != null) {
					sSynonym = (String) sSessionSyn;
				} else {
					sSynonym = oLoginData.getLoggedInSynonym();
				}
			}
			int iCurMsgIndex = oLoginData.getCurReqIndex();
			List lMessages = IContact.getRequestsBySynonym(sSynonym,
					iCurMsgIndex * MAX_MSGS_COUNT, MAX_MSGS_COUNT);

			List[] resList = new List[MAX_MSGS_COUNT];
			for (int reqCnt = 0; reqCnt < lMessages.size(); reqCnt++) {
				resList[reqCnt] = IContact.getReplies((MessageData) lMessages
						.get(reqCnt), 0, IContact
						.getReplyCount((MessageData) lMessages.get(reqCnt)));
			}

			oRequest.getSession(false).setAttribute(SESSION_SYN, sSynonym);
			oRequest.setAttribute(REQ_MSG_LIST, lMessages);
			oRequest.setAttribute(SESSION_REQ_RES, resList);

			oRequest.getSession().setAttribute(SESSION_REQ_COUNT,
					IContact.getMsgCountBySynonym(sSynonym));
		} catch (Exception e) {
			oLogger.error("process() : Exception : " + e.getMessage());
		}
		oLogger.info("process() : forward to: " + FWD_SUCCESS);
		return oMapping.findForward(FWD_SUCCESS);
	}
}
